// scripts/api-check.mjs
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';

const packagesDir = path.resolve('./packages');
const packages = fs.readdirSync(packagesDir, { withFileTypes: true })
  .filter(dirent => dirent.isDirectory())
  .map(dirent => dirent.name);

for (const pkg of packages) {
  const pkgPath = path.join(packagesDir, pkg);
  const configPath = path.join(pkgPath, 'api-extractor.json');

  if (fs.existsSync(configPath)) {
    console.log(`\n=== Running API Extractor for package: ${pkg} ===\n`);
    try {
      execSync(`npx api-extractor run --local --verbose --config "${configPath}"`, {
        stdio: 'inherit'
      });
    } catch (err) {
      console.error(`API Extractor failed for package: ${pkg}`);
      process.exit(1);
    }
  }
}
